package org.jeecg.cn.edu.scau.vm.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.jeecg.cn.edu.scau.vm.common.consts.MQConst.QueueName.*;

@Configuration
public class RabbitConfig {
    private static final Logger logger = LoggerFactory.getLogger(RabbitConfig.class);

    @Value("${spring.rabbitmq.host}")
    private String host;
    @Value("${spring.rabbitmq.username}")
    private String username;
    @Value("${spring.rabbitmq.password}")
    private String password;
    @Value("${spring.rabbitmq.port}")
    private String port;
    @Value("${spring.rabbitmq.virtual-host}")
    private String virtualHost;


    @Bean
    public ConnectionFactory connectionFactory() {
        logger.info("RabbitMq连接信息,host:{},port:{},username:{},password:{},virtualHost:{}",
                host, port, username, password, virtualHost);
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(Integer.parseInt(port));
        connectionFactory.setPassword(password);
        connectionFactory.setUsername(username);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean("containerCreateSuccessQueue")
    public Queue containerCreateSuccessQueue() {
        logger.info("建立创建容器成功的队列:{}", CONTAINER_CREATE_SUCCESS_QUEUE);
        return new Queue(CONTAINER_CREATE_SUCCESS_QUEUE, true, false, false);
    }

    @Bean("containerCreateFailedQueue")
    public Queue containerCreateFailedQueue() {
        logger.info("建立创建容器失败的队列:{}", CONTAINER_CREATE_FAILED_QUEUE);
        return new Queue(CONTAINER_CREATE_FAILED_QUEUE, true, false, false);
    }

    @Bean("containerStopSuccessQueue")
    public Queue containerStopSuccessQueue() {
        logger.info("建立暂停容器成功的队列:{}", CONTAINER_STOP_SUCCESS_QUEUE);
        return new Queue(CONTAINER_STOP_SUCCESS_QUEUE, true, false, false);
    }

    @Bean("containerStopFailedQueue")
    public Queue containerStopFailedQueue() {
        logger.info("建立暂停容器失败的队列:{}", CONTAINER_STOP_FAILED_QUEUE);
        return new Queue(CONTAINER_STOP_FAILED_QUEUE, true, false, false);
    }

    @Bean("containerStartSuccessQueue")
    public Queue containerStartSuccessQueue() {
        logger.info("建立启动容器成功的队列:{}", CONTAINER_START_SUCCESS_QUEUE);
        return new Queue(CONTAINER_START_SUCCESS_QUEUE, true, false, false);
    }

    @Bean("containerStartFailedQueue")
    public Queue containerStartFailedQueue() {
        logger.info("建立启动容器失败的队列:{}", CONTAINER_START_FAILED_QUEUE);
        return new Queue(CONTAINER_START_FAILED_QUEUE, true, false, false);
    }

    @Bean("containerRemoveSuccessQueue")
    public Queue removeCreateSuccessQueue() {
        logger.info("建立删除容器成功的队列:{}", CONTAINER_REMOVE_SUCCESS_QUEUE);
        return new Queue(CONTAINER_REMOVE_SUCCESS_QUEUE, true, false, false);
    }

    @Bean("containerRemoveFailedQueue")
    public Queue removeCreateFailedQueue() {
        logger.info("建立删除容器失败的队列:{}", CONTAINER_REMOVE_FAILED_QUEUE);
        return new Queue(CONTAINER_REMOVE_FAILED_QUEUE, true, false, false);
    }


}
